package com.example.xjtuse_pc.mobilesafe;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.Toast;

public class RockerActivity extends Activity {
    private ImageView iv;
    private ImageView iv_bottom;
    private ImageView iv_top;
    private AnimationDrawable rocketAnimation;
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            int position = (Integer) msg.obj;
            iv.layout(iv.getLeft(), position, iv.getRight(), position + iv.getHeight());
            if (position < 320) {
                iv_top.setVisibility(View.VISIBLE);
                AlphaAnimation aa = new AlphaAnimation(0.6f, 1.0f);
                aa.setDuration(400);
                iv_top.startAnimation(aa);
            }
            if (position < 20) {
                AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f);
                aa.setDuration(400);
                aa.setFillAfter(true);
                iv_top.startAnimation(aa);
            }

        }

        ;
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rocker);
        iv = (ImageView) findViewById(R.id.iv);
        iv.setBackgroundResource(R.drawable.rocket);
        iv_top = (ImageView) findViewById(R.id.iv_top);
        iv_bottom = (ImageView) findViewById(R.id.iv_bottom);
        rocketAnimation = (AnimationDrawable) iv.getBackground();
        rocketAnimation.start();
        iv.setOnTouchListener(new View.OnTouchListener() {
            int startX;
            int startY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int newX = (int) event.getRawX();
                        int newY = (int) event.getRawY();
                        int dx = newX - startX;
                        int dy = newY - startY;
                        Log.i("join",newX+":"+ newY);
                        iv.layout(iv.getLeft() + dx, iv.getTop() + dy, iv.getRight() + dx, iv.getBottom() + dy);
                        Log.i("join", iv.getTop() + ";" + iv.getLeft() + ";" + iv.getRight());
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_UP:
                        int top = iv.getTop();
                        int left = iv.getLeft();
                        int right = iv.getRight();

                        if (top > 300 && left > 100 && right > 220) {
                            Toast.makeText(getApplicationContext(), "发射火箭", Toast.LENGTH_SHORT).show();
                            iv_bottom.setVisibility(View.VISIBLE);
                            AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
                            aa.setDuration(500);
                            aa.setRepeatCount(1);
                            aa.setRepeatMode(Animation.REVERSE);
                            aa.setFillAfter(true);
                            iv_bottom.startAnimation(aa);
                            sendRocket();
                        }
                        break;
                }
                return true;
            }
        });
    }

    private void sendRocket() {
        new Thread(){
        			public void run() {
        				int start = 380;
        				for(int i = 0 ;i<=11;i++){
        					try {
        						Thread.sleep(10);
        					} catch (InterruptedException e) {
        						e.printStackTrace();
        					}
        					//更新一下ui
        					Message msg = Message.obtain();
        					msg.obj = 380 - i*38;//计算出火箭的高度
        					handler.sendMessage(msg);
        				}
        			};
        		}.start();
    }


}
